//---------------------------------------------------------------------------
/**
 * \class CLog
 * \brief This is a class for logging
 * \version 0.1
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2008/09/22
 */
//---------------------------------------------------------------------------
#ifndef CLogH
#define CLogH
//-----------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <map>
#include <algorithm>
#include <string>

#include <sys/timeb.h>
#include <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>

#define DEFAULT_LOG_FILE "/tmp/wooh.log"

using namespace std;
//---------------------------------------------------------------
class CLog {
private :
	ostringstream out;

	string logfullfile; // a fully file name with file path and file ext
	string logpath; // only file path
	string logext; // only file ext

public :
	CLog();
	CLog(char *file);
	virtual ~CLog();

	int print(char *msg);
	int print(char *msg, long line_num, char *source_file);

	template <typename T>
	friend CLog & operator<<(CLog& log, const T& val);
	friend CLog & operator<<(CLog& log, ostream& (*el)(ostream&));
};
//----------------------------------------------------------------
// cout-like << operator overload
//--------------------------------------------------------------------------
template <typename T>
inline CLog &operator<<(CLog &log, const T& val)
{
	log.out << val;

	return log;
}
//--------------------------------------------------------------------------
// for std::endl
//--------------------------------------------------------------------------
inline CLog &operator<<(CLog &log, ostream& (*el)(ostream&))
{
	// std::endl looks like a function
	//(*el)(log.out);

	log.print((char *)log.out.str().c_str());

	// clear log.out
	log.out.str("");

	return log;
}
//--------------------------------------------------------------------------
#endif
